package edu.scau.mis.system.service.impl;

import edu.scau.mis.system.domain.User;
import edu.scau.mis.system.security.domain.LoginUser;
import edu.scau.mis.system.security.service.TokenService;
import edu.scau.mis.system.service.IMenuService;
import edu.scau.mis.system.service.IRoleService;
import edu.scau.mis.system.service.IUserAuthService;
import edu.scau.mis.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class UserAuthServiceImpl implements IUserAuthService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return
     * @throws AuthenticationException
     */
    @Override
    public Map<String, Object> login(String username, String password) throws AuthenticationException {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        String accessToken = (String)tokenService.createAccessToken(loginUser).get("token");
        String refreshToken = (String)tokenService.createRefreshToken(loginUser).get("token");
        User user = loginUser.getUser();
        Map<String, Object> data = new HashMap<>();
        data.put("userName",user.getUserName());
        data.put("avatar",user.getAvatar());
        data.put("nickName",user.getNickName());
        data.put("roles",roleService.selectRoleKeysByUserId(user.getUserId()) );
        data.put("permissions",menuService.selectMenuPermsByUserId(user.getUserId()));
        // JWT token
        data.put("accessToken", accessToken);
        data.put("refreshToken", refreshToken);

        // 格式化过期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        data.put("expires", sdf.format(tokenService.createAccessToken(loginUser).get("expireTime")));
        return data;
    }

    @Override
    public Map<String, Object> refreshToken(String refreshToken) throws AuthenticationException {
        return null;
    }


}